var selectvalue = -1;
var certArray = null;
var zjcaClient = null;

$(document).on('click', '#btn_no', function(){
    selectvalue = -1;
    closeWindow();
});

$(document).on('click', '.closeicon', function(){
    selectvalue = -1;
    closeWindow();
});
//关闭证书选择窗口
function closeWindow(){
    $(".keyBtn,.certBtn").removeAttr('disabled');
    $("#oWindow").hide();
};

//显示证书选择对话框
function dlg_zcjaShowCertSelect(my_cert, zjcaCMT) {
	certArray = my_cert;
	zjcaClient = zjcaCMT;
	
    // 删除老的选项
    var obs = $(".obs");
    $(".obs > div").remove();
    if (my_cert && my_cert.length > 0) {
        for (var n = 0; n < my_cert.length; n++) {
            obs.append('<div class="cin" data-cert="' + n + '"><div class="clogo"></div><div class="cinfo"><p class="cinfot fs16">' + my_cert[n].getSubjectCN() + '</p><p class="cinfot">颁布者：' + my_cert[n].getIssuerCN() + '</p><p class="cinfot">有效期：' + my_cert[n].getFrom() + ' 至 ' + my_cert[n].getUntil() + '</p><p class="cinfot"></p><p class="cinfot"><a href="javascript:void(0)" class="infoHover" id="certInfo">单击此处查看证书属性</a></p></div></div>');
        }
        //默认选中第一张证书
        $(".cin:first").addClass("cinh");
        selectvalue = $(".cinh").attr("data-cert");
    }else {
        obs.append('<div id="selectcert" value="-1">未发现合法证书</div>');
    }
     $("#oWindow").show();
}
//选中及hover样式
$(document).on('click', '.cin', function(event) {
    $(".cin").each(function(){
        $(this).removeClass("cinh");
    });
    $(this).addClass("cinh");
    selectvalue = $(this).attr("data-cert");
});

//返回选择证书的值
function dlg_zcjaGetSelectedCert() {
	closeWindow();
	return Number(selectvalue);
}
//显示证书详情
$(document).on('click', '.sameWindow a', function(){
    $(".cinh").removeClass("cinh");
    $(this).parents(".cin").addClass("cinh");
    selectvalue = $(this).parents(".cinh").attr("data-cert");
    
	//显示选中的证书详细信息
	if (zjcaClient && certArray) {
		var selIndex = Number(selectvalue);
		if (selIndex >= 0 && selIndex < certArray.length) {
			var resp = zjcaClient.getCertContentBySN(certArray[selIndex].getSN());
			if (0 == resp.code) {
				zjcaClient.showCert(resp.res, 0);
			}			
		}
	}    
});

//判断浏览器
$(function(){
    var sUserAgent = navigator.userAgent;
    var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
    var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
     
    if (isWin) {
        $('head').append('<link rel="stylesheet" type="text/css" href="css/Windows.css">')
    }else{
        $('head').append('<link rel="stylesheet" type="text/css" href="css/Linux.css">')
    }
});
